package org.utils.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;

import org.utils.common.InitException;

/**
 * @author Manuel EVENO
 */
public class AboutDialog extends JDialog
{
	private static final long serialVersionUID = -7131476832866813614L;

	/** A default border (empty, 5 pixels on all sides). */
	public static final EmptyBorder defaultBorder = new EmptyBorder(5, 5, 5, 5);
	private JPanel contentPane = null;
	private LayoutManager contentLayout = new BorderLayout(5, 5);
	private JScrollPane scroller = new JScrollPane();
	private JEditorPane editor = new JEditorPane();
	private ButtonPanel buttonPanel = new ButtonPanel();
	private JButton okButton = new JButton();
	private URL content = null;
	/**
	 * Constructor for AboutDialog.
	 * @param parent the parent Frame
	 * @param pContent The page to show
	 * @throws HeadlessException
	 */
	public AboutDialog(Frame parent, URL pContent) throws InitException
	{
		super(parent);
		try
		{
			content = pContent;			
			jbInit();
		}
		catch (Exception e)
		{
			throw new InitException(e);
		}
	}
	/**
	 * Constructor for AboutDialog.
	 * @param parent the parent Frame
	 * @param title the dialog title
	 * @param pContent The page to show
	 * @throws HeadlessException
	 */
	public AboutDialog(Frame parent, String title, URL pContent) throws InitException
	{
		this(parent, pContent);
		setTitle(title);
	}
	/**
	 * Method jbInit.
	 */
	private void jbInit() throws Exception
	{
		contentPane = (JPanel) getContentPane();

		editor.setPage(content);
		editor.setEditable(false);
		setTitle( (String) editor.getDocument().getProperty(Document.TitleProperty) );
		
		okButton.setText("OK");
		buttonPanel.addButton(okButton);
		buttonPanel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
		buttonPanel.buildPanel();

		scroller.getViewport().add(editor);

		contentPane.setLayout(contentLayout);
		contentPane.add(scroller, BorderLayout.CENTER);
		contentPane.add(buttonPanel, BorderLayout.SOUTH);
		contentPane.setBorder(defaultBorder);
		pack();
	}
}
